package com.hermes.util.file;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.hermes.constant.ConstantParams;


public class ImgProducer {
	public static void inita() throws IOException{		
		URL rootURL = ImgProducer.class.getResource("/");
		String rootPath = rootURL.getPath().toString();		
		//ImgProducer.createSmallSquarePic(rootPath+"images\\item\\tag1");
		//change codes below to xml configure file to read 
		List<String> tagsFolder = new ArrayList<String>();
		tagsFolder.add(rootPath+"images\\item\\tag1");
		tagsFolder.add(rootPath+"images\\item\\tag2");
		tagsFolder.add(rootPath+"images\\item\\tag3");
		tagsFolder.add(rootPath+"images\\item\\tag4");

	//调试快，先去掉生产小图

		for(int i = 0;i<tagsFolder.size();i++){	
			//create a*a pic for detail
			createSmallSquarePic(tagsFolder.get(i));
			//create a*b ratio pic for show :index search
			createPropotionPic(tagsFolder.get(i));
		}
	}
	//for detail a*a
	public static boolean createSmallSquarePic(String path){
		List<String> fileNames = FileUtil.getFile(path);
		for(int i = 0;i<fileNames.size();i++){
			FileUtil.reduceImg(fileNames.get(i), getDistPath(fileNames.get(i)), ConstantParams.detail_small_size_x, ConstantParams.detail_small_size_y);
		}
		return false;
	}
	//for show a*b
	public static boolean createPropotionPic(String path) throws IOException{
		List<String> fileNames = FileUtil.getFile(path);
		for(int i = 0;i<fileNames.size();i++){
			String file = fileNames.get(i);
			BufferedImage bufferedImage = ImageIO.read(new File(file));   
			int width = bufferedImage.getWidth();   
			int height = bufferedImage.getHeight();   
			float rat = (float)ConstantParams.show_img_with/(float)width;
			int widthAfter = ConstantParams.show_img_with;
			int heigtAfter = (int) (height*rat);
			FileUtil.reduceImg(fileNames.get(i), getDistPathForShow(file), widthAfter, heigtAfter);
			//FileUtil.scale(fileNames.get(i), getDistPathForShow(file), 1, false);
		}
		return false;
	}
	//get dest path for show
	private static String getDistPathForShow(String srcPath){		
		String fileName = StringUtils.substringAfterLast(srcPath, "\\");
		String filePath = StringUtils.substringBeforeLast(srcPath, "\\");
		String lastFolder = StringUtils.substringAfterLast(filePath, "\\");
		String preLastFolder = StringUtils.substringBeforeLast(filePath, "\\");
		String destFolder = preLastFolder+"\\"+lastFolder+"m\\";
		if(new File(destFolder).exists()){
			
		}else{
			File file = new File(destFolder);
			file.mkdir();
		}
		String destPathFile = preLastFolder+"\\"+lastFolder+"m\\"+fileName;
		return destPathFile;
	}
	//
	private static String getDistPath(String srcPath){		
		String fileName = StringUtils.substringAfterLast(srcPath, "\\");
		String filePath = StringUtils.substringBeforeLast(srcPath, "\\");
		String lastFolder = StringUtils.substringAfterLast(filePath, "\\");
		String preLastFolder = StringUtils.substringBeforeLast(filePath, "\\");
		String destFolder = preLastFolder+"\\"+lastFolder+"s\\";
		if(new File(destFolder).exists()){
			
		}else{
			File file = new File(destFolder);
			file.mkdir();
		}
		String destPathFile = preLastFolder+"\\"+lastFolder+"s\\"+fileName;
		return destPathFile;
	}
	
}
